package cn.com.gridinfo.sooc.core.my.msg.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jeremy.arad.utils.AnimUtil;
import com.marshalchen.ultimaterecyclerview.UltimateRecyclerviewViewHolder;
import com.marshalchen.ultimaterecyclerview.UltimateViewAdapter;

import java.util.List;

import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.core.course.classes.activity.CourseDetailActivity;
import cn.com.gridinfo.sooc.core.my.msg.bean.CourseMsg;
import cn.com.gridinfo.sooc.utils.widget.MyExpandTextView;


/**
 * 课程通知适配器
 * Created by liuxiaochen on 2016/1/7.
 */
public class CourseMsgAdapter extends UltimateViewAdapter<CourseMsgAdapter.ViewHolder> {
    private List<CourseMsg> courseMsgsList;
    private ViewHolder viewHolder;
    private Context context;

    public CourseMsgAdapter(List<CourseMsg> list, Context context) {
        this.courseMsgsList = list;
        this.context = context;
    }

    @Override
    public ViewHolder getViewHolder(View view) {
        return new ViewHolder(view, false);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent) {
        //加载布局
        View v = LayoutInflater.from
                (parent.getContext()).inflate(R.layout.my_course_msg, parent, false);
        return new ViewHolder(v, true);
    }

    @Override
    public int getAdapterItemCount() {
        return courseMsgsList.size();
    }

    @Override
    public long generateHeaderId(int position) {
        return position;
    }

    @Override
    public void onBindViewHolder(final CourseMsgAdapter.ViewHolder holder, int position) {
        if (position < getItemCount() && (customHeaderView != null ?
                position <= courseMsgsList.size() : position < courseMsgsList.size()) &&
                (customHeaderView != null ? position > 0 : true)) {
            final CourseMsg courseMsg = courseMsgsList.get(position);
            holder.tznr.setText(courseMsg.getTznr());//通知内容
            holder.kcmc.setText(courseMsg.getKcmc());//课程名称
            if (position == 0) {
                holder.input_time.setText(courseMsg.getSj());//显示的加入时间
                holder.input_time.setVisibility(View.VISIBLE);
            } else {
                CourseMsg courseMsg1 = courseMsgsList.get(position - 1);
                if (courseMsg.getSj().equals(courseMsg1.getSj())) {
                    holder.input_time.setVisibility(View.GONE);
                } else {
                    holder.input_time.setVisibility(View.VISIBLE);
                    holder.input_time.setText(courseMsg.getSj());
                }
            }

            //点击查看课程 跳转到相应的课程详情界面
            holder.chakan.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, CourseDetailActivity.class);
                    intent.putExtra("kcid", courseMsg.getKcid());
                    context.startActivity(intent);
                    AnimUtil.intentSlidIn((Activity) context);//加动画
                }
            });
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        return null;
    }

    public void loadmore(List<CourseMsg> list) {
        courseMsgsList.addAll(list);
        notifyDataSetChanged();
    }

    public void refresh(List<CourseMsg> list) {
        clear(courseMsgsList);
        courseMsgsList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {

    }

    public class ViewHolder extends UltimateRecyclerviewViewHolder {
        TextView input_time;
        MyExpandTextView tznr;
        TextView kcmc;
        RelativeLayout chakan;

        public ViewHolder(View itemView, boolean isItem) {
            super(itemView);
            if (isItem) {
                input_time = (TextView) itemView.findViewById(R.id.time);//加入时间
                tznr = (MyExpandTextView) itemView.findViewById(R.id.expand_text_view);//通知内容
                kcmc = (TextView) itemView.findViewById(R.id.kcmc);//课程名称
                chakan = (RelativeLayout) itemView.findViewById(R.id.chakan_rl);//查看 布局 监听点击
            }
        }
    }
}
